<?php

namespace app\web\filter;

use app\common\component\Param;
use app\common\component\ErrorCode;
use app\common\component\Redis;

/**
 * 过滤条件条件类
 * Class IndexFilter
 * @package app\web\filter
 */
class IndexFilter
{
    /**
     * xss过滤
     * @param $string
     * @return mixed|null|string|string[]
     */
    private function cleanXss($string)
    {
        $string = trim($string);
        $string = strip_tags($string);
//        $string = htmlspecialchars ( $string );
//        $string = str_replace ( array ('"', "\\", "'", "/", "..", "../", "./", "//" ), '', $string );
        $no = '/%0[0-8bcef]/';
        $string = preg_replace($no, '', $string);
        $no = '/%1[0-9a-f]/';
        $string = preg_replace ($no, '', $string);
        $no = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S';
        $string = preg_replace ( $no, '', $string );

        return $string;
    }

    /**
     * 生成word方法入参校验
     * @throws \think\Exception
     */
    public function formWord()
    {
        if (!isset(Param::$inputParams['content'])) {
            throw new \think\Exception('参数格式错误', ErrorCode::RESPONSE_FAIL);
        }

        if (!is_string(Param::$inputParams['content'])) {
            throw new \think\Exception('content参数值错误', ErrorCode::RESPONSE_FAIL);
        }

        Param::$validParams['content'] = Param::$inputParams['content'];
    }

    /**
     * elsaticsearch热词入参校验
     * @throws \think\Exception
     */
    public function addElasticsearch()
    {
        if (!isset(Param::$inputParams['content'])) {
            throw new \think\Exception('参数格式错误', ErrorCode::RESPONSE_FAIL);
        }

        if (!is_string(Param::$inputParams['content']) || mb_strlen(Param::$inputParams['content'], 'utf-8') > 500) {
            throw new \think\Exception('内容超过500自负', ErrorCode::RESPONSE_FAIL);
        }
        Param::$validParams['content'] = $this->cleanXss(Param::$inputParams['content']);

        if (!is_string(Param::$inputParams['ela_identify']) || Param::$inputParams['ela_identify'] == '') {
            throw new \think\Exception('ela_identify参数值错误', ErrorCode::RESPONSE_FAIL);
        }
        Param::$validParams['ela_identify'] = $this->cleanXss(Param::$inputParams['ela_identify']);
    }

    /**
     * 列表数据
     * @throws \think\Exception
     */
    public function listElasticsearch()
    {
        if (!isset(Param::$inputParams['noncestr'], Param::$inputParams['ela_identify'])) {
            throw new \think\Exception('参数格式错误', ErrorCode::RESPONSE_FAIL);
        }

        $noncestr = substr(Param::$inputParams['noncestr'], 0, -2);
        if (Redis::getInstance()->get($noncestr) === false) {
            throw new \think\Exception('凭证错误,请刷新页面', ErrorCode::RESPONSE_FAIL);
        }

        if (!is_string(Param::$inputParams['ela_identify']) || Param::$inputParams['ela_identify'] == '') {
            throw new \think\Exception('ela_identify参数值错误', ErrorCode::RESPONSE_FAIL);
        }
        Param::$validParams['ela_identify'] = $this->cleanXss(Param::$inputParams['ela_identify']);
    }

    /**
     * 生成excel文件
     * @throws \think\Exception
     */
    public function formatExcel()
    {
        if (!isset(Param::$inputParams['excel_data'])) {
            throw new \think\Exception('参数格式错误', ErrorCode::RESPONSE_FAIL);
        }

        if (!is_array(Param::$inputParams['excel_data'])) {
            throw new \think\Exception('excel_data参数格式错误', ErrorCode::RESPONSE_FAIL);
        }

        foreach (Param::$inputParams['excel_data'] as $excel_item) {
            if (!is_array($excel_item)) {
                throw new \think\Exception('excel_data的子元素格式错误', ErrorCode::RESPONSE_FAIL);
            }
        }
        Param::$validParams['excel_data'] = Param::$inputParams['excel_data'];
    }
}